package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Book;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.BookService;
import com.tbit.uqbike.service.business.UserService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约记录
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Validated
@RestController
@RequestMapping(value = "/book", produces = BaseController.MEDIA_JSON_UTF8)
public class BookController extends BaseController {
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private AccountService accountService;

    /* 分页查询 */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(@RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "1") Integer pageNO,
                                Integer userId, String phone, String machineNO,
                                String startTime, String endTime,
                                HttpServletRequest request) {
        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        Integer accountId;
        if (loginInfo.type.equals(UserConstant.USER_ADMIN)) {
            accountId = null;
        } else if (loginInfo.type.equals(UserConstant.USER_BRAND)) {
            accountId = loginInfo.id;
        } else {
            Assert.notNull(machineNO, "ver.machineNo.notNull");
            accountId = accountService.getById(loginInfo.id).getParentId();
        }
        List<Integer> userIds = new ArrayList<>();
        List<User> users;
        if (phone != null) {
            users = userService.getByAccountIdAndPhone(accountId, phone);
            if (users == null || users.size() < 1) {
                throw new BaseException(SystemCodeEnum.USER_NOT_EXIT);
            } else {
                for (User user : users) {
                    userIds.add(user.getUserId());
                }
            }
        }
        Integer machineId = null;
        if (machineNO != null && machineNO.length() > 0) {
            Machine machine = machineService.getByMachineNO(machineNO);
            if (machine == null) {
                throw new BaseException("ver.machine.notNull");
            } else {
                machineId = machine.getMachineId();
            }
        }
        /*获取总数*/
        Integer total = bookService.queryCount(userIds, machineId, startTime, endTime);
        /*当前返回行数*/
        List<Book> bookLogs = bookService.queryPage(userIds, machineId, startTime, endTime, rowCount, pageNO);
        /**设置手机号码*/
        if (bookLogs.size() > 0) {
            if (userId != null) {
                User user = userService.getByUserId(userId);
                for (Book book : bookLogs) {
                    book.setPhone(user.getPhone());
                }
            } else if (machineId != null) {
                List<Integer> uIds = new ArrayList<Integer>();
                for (Book book : bookLogs) {
                    if (!uIds.contains(book.getUserId())) {
                        uIds.add(book.getUserId());
                    }
                }
                if (uIds.size() > 0) {
                    List<User> us = userService.getByUserIdBatch(uIds);
                    Map<Integer, String> userPhoneMap = new HashMap<Integer, String>();
                    for (User user : us) {
                        userPhoneMap.put(user.getUserId(), user.getPhone());
                    }
                    for (Book book : bookLogs) {
                        if (userPhoneMap.containsKey(book.getUserId())) {
                            book.setPhone(userPhoneMap.get(book.getUserId()));
                        }
                    }
                }
            }
        }

        return JsonResult.ok(bookLogs, total);
    }
}